package com.vcaresolution.logistic.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;

import com.vcaresolution.logistic.pojo.User;
import com.vcaresolution.logistic.service.ServiceRegistry;
import com.vcaresolution.logistic.to.UserProfileTO;
import com.vcaresolution.logistic.util.ApplicationConstants;
import com.vcaresolution.logistic.util.CustomDateEditor;
import com.vcaresolution.logistic.util.CustomIntegerEditor;

/**
 * The <code>BaseController</code> is a Base Class for all Controller in <b>LEADER</b>
 * application.
 *
 * @author Snehal Shah
 * @version 1.0
 * @since ShipSoft 1.0
 */
@Controller
public class BaseController {

	@Autowired
	private MessageSource messageSource;

	private ServiceRegistry serviceRegistry;

	/**
	 * This method gives access to Service Registry
	 *
	 * @return ServiceRegistry
	 */
	protected ServiceRegistry getServiceRegistry() {
		return serviceRegistry;
	}

	/**
	 * This method is a setter method for Service Registry. This will be called by Spring Context
	 *
	 * @param serviceRegistry
	 */
	@Autowired
	public final void setServiceRegistry(final ServiceRegistry serviceRegistry) {
		this.serviceRegistry = serviceRegistry;
	}

	/**
	 * This method is used for getting error message(From MessageSource) from BindingResult
	 * @param result
	 * @return
	 */
	protected List<String> getFieldErrorMessages(final BindingResult result) {
		final List<FieldError> fieldErrors = result.getFieldErrors();
		List<String> actualMessages = null;

		if (fieldErrors != null && fieldErrors.size() > 0) {
			actualMessages = new ArrayList<String>();
			String message = null;

			for (final FieldError error : fieldErrors) {
				message = messageSource.getMessage(error, Locale.getDefault());
				System.out.println("Message :: "+message);
				
				if (message != null) {
					actualMessages.add(message);
					message = null;
				}
			}
		}

		return actualMessages;
	}

	/**
	 * This method gives access to MessageSource
	 *
	 * @return MessageSource
	 */
	protected MessageSource getMessageSource() {
		return messageSource;
	}
	
	protected UserProfileTO getLoggedInUserProfileTO(HttpServletRequest request){
		return getLoggedInUserProfileTO(request.getSession());
	}
	
	protected UserProfileTO getLoggedInUserProfileTO(HttpSession session){
		return (UserProfileTO) session.getAttribute(ApplicationConstants.USERPROFILE_SESSION_OBJECT);
	}
	
	protected User getLoggedInUser(HttpServletRequest request){
		return getLoggedInUser(request.getSession());
	}
	
	protected User getLoggedInUser(HttpSession session){
		UserProfileTO userProfile = (UserProfileTO) session.getAttribute(ApplicationConstants.USERPROFILE_SESSION_OBJECT);
		return serviceRegistry.getUserService().findById(userProfile.getUserid());
	}
	
	/**
	 * @param binder
	 */
	@InitBinder
	protected void initBinder(final WebDataBinder binder) {
		/**
		 * Create a new CustomDateEditor
		 */
		final CustomDateEditor dateEditor = new CustomDateEditor(
				ApplicationConstants.DEFAULT_DDMMYYYY_DATE_FORMAT, true);

		/**
		 * Register it as custom editor for the java.util.Date type
		 */
		binder.registerCustomEditor(Date.class, dateEditor);

		/**
		 * Create a new CustomIntegerEditor
		 */
		final CustomIntegerEditor integerEditor = new CustomIntegerEditor(true);

		/**
		 * Register it as custom editor for the java.lang.Integer type
		 */
		binder.registerCustomEditor(Integer.class, integerEditor);
	}
}
